package com.byxy.mysystem.controller.testUser;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byxy.mysystem.model.testUser.TestUser;
import com.byxy.mysystem.service.testUser.ITestUserService;
import com.byxy.mysystem.util.JsonResult;
import com.byxy.mysystem.util.ResultTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  面试测试用户前端控制器
 * </p>
 *
 * @author omd
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/testUser")
public class TestUserController {

    @Autowired
    private ITestUserService testUserService;


    /**
     * 面试测试用户分页接口
     * @param params
     * @return
     */
    @PostMapping("/list")
    public JsonResult<IPage<TestUser>> list(@RequestBody Map<String,Object> params){
        long current = params.get("current") != null?Integer.valueOf(params.get("current").toString()):1;
        long size = params.get("size") != null?Integer.valueOf(params.get("size").toString()):10;
        IPage page = new Page(current,size);
        QueryWrapper<TestUser> wrapper = new QueryWrapper<>();
        if(params.get("keyWord") != null && StringUtils.isNotBlank(params.get("keyWord").toString())){
            wrapper.like("name",params.get("keyWord").toString());
        }
        wrapper.orderByDesc("id");
        IPage<TestUser> result = testUserService.page(page, wrapper);
        return ResultTool.success(result);
    }

    @PostMapping("/view")
    public JsonResult view(@RequestBody TestUser testUser){
        TestUser testUser1 = testUserService.getById(testUser.getId());
        return ResultTool.success(testUser1);
    }

    /**
     * 面试测试用户保存接口
     * @param testUser
     * @return
     */
    @PostMapping("/save")
    public JsonResult save(@RequestBody TestUser testUser){
        boolean flag = testUserService.save(testUser);
        return ResultTool.success(flag);
    }

    /**
     * 面试测试用户更新接口
     * @param testUser
     * @return
     */
    @PostMapping("/update")
    public JsonResult update(@RequestBody TestUser testUser){
        boolean flag = testUserService.updateById(testUser);
        return ResultTool.success(flag);
    }

    /**
     * 面试测试用户根据id删除接口
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public JsonResult save(@RequestBody Map<String,Object> param){
        boolean flag = false;
        if(param != null && StringUtils.isNotBlank(param.get("ids").toString())){
            String idsStr = param.get("ids").toString();
            List<Long> ids= JSONObject.parseObject(idsStr,List.class);
            flag = testUserService.removeByIds(ids);
        }
        return ResultTool.success(flag);
    }


}
